<template>
  <view>
    <view class="search-box"><my-search @click.native="gotoSearch"></my-search></view>
    <!-- 轮播图区域 -->
    <swiper indicator-dots autoplay interval="3000" duration="1000" circular>
      <swiper-item v-for="(item, index) in swiperList" :key="item.goods_id">
        <navigator :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`"><image :src="item.image_src"></image></navigator>
      </swiper-item>
    </swiper>
    <!-- 导航区域 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, index) in navList" :key="index" @click="navClickHandler(item.name)"><image :src="item.image_src"></image></view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <image :src="item.floor_title.image_src"></image>
        <!-- 左侧大图区域 -->
        <view class="floor-img-box">
          <view class="left-img-box">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
              @click="gotoGoodsList(item.product_list[0])"
            ></image>
          </view>
          <!-- 右侧小图区域 -->
          <view class="right-img-box">
            <image
              :src="item.image_src"
              mode="widthFix"
              :style="{ width: item.image_width + 'rpx' }"
              v-if="index > 0"
              v-for="(item, index) in item.product_list"
              :key="index"
              @click="gotoGoodsList(item)"
            ></image>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import tabbarBadge from '@/mixins/tabbar-badge.js';
export default {
  name: 'HomePage',
  mixins: [tabbarBadge],
  components: {},
  data() {
    return {
      swiperList: [], // 轮播图数据
      navList: [], // 导航数据
      floorList: [] // 楼层数据
    };
  },
  created() {},
  mounted() {},
  onLoad() {
    this.getSwiperList();
    this.getNavList();
    this.getFloorList();
  },
  methods: {
    gotoSearch() {
      uni.navigateTo({
        url: '/subpkg/search/search'
      });
    },
    async getFloorList() {
      let {
        data: {
          message,
          meta: { status }
        }
      } = await uni.$http.get('/api/public/v1/home/floordata');
      if (status !== 200) {
        return uni.$showMsg();
      }
      this.floorList = message;
    },
    async getNavList() {
      let {
        data: {
          message,
          meta: { status }
        }
      } = await uni.$http.get('/api/public/v1/home/catitems');
      if (status !== 200) {
        return uni.$showMsg();
      }
      this.navList = message;
    },
    async getSwiperList() {
      let {
        data: {
          message,
          meta: { status }
        }
      } = await uni.$http.get('/api/public/v1/home/swiperdata');
      if (status !== 200) {
        return uni.$showMsg();
      }
      this.swiperList = message;
    },
    navClickHandler(val) {
      if (val === '分类') {
        uni.switchTab({
          url: '/pages/cate/cate'
        });
      }
    },
    gotoGoodsList(item) {
      uni.navigateTo({
        url: '/subpkg/goods_list/goods_list' + item.navigator_url.substr(item.navigator_url.indexOf('?'))
      });
    }
  }
};
</script>

<style lang="scss">
.search-box {
  position: sticky;
  top: 0px;
  z-index: 999;
}
// 轮播图样式
swiper {
  height: 330rpx;
  swiper-item {
    navigator,
    image {
      width: 100%;
      height: 100%;
    }
  }
}
// 导航区域样式
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;
  .nav-item {
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }
}
.floor-item {
  image {
    height: 60rpx;
    width: 100%;
  }
}
.floor-img-box {
  display: flex;
  padding: 0 10rpx;
  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
}
</style>
